package com.example.service.impl;

import com.example.algorithm.SnowflakeIdWorker;
import com.example.entity.Collection;
import com.example.entity.Sentence;
import com.example.entity.Topic;
import com.example.repository.CollectionRepository;
import com.example.repository.SentenceRepository;
import com.example.repository.TopicRepository;
import com.example.service.CollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class CollectionServiceImpl implements CollectionService {

    @Autowired
    private CollectionRepository collectionRepository;
    @Autowired
    private TopicRepository topicRepository;
    @Autowired
    private SentenceRepository sentenceRepository;

    @Override
    public Map<String, List<Collection>> getCollectionsByUserId(String userId){
        Map<String, List<Collection>> result = new HashMap();
        List<Collection> list = collectionRepository.findByUserId(userId);
        Sentence sentence;
        Topic topic;
        List<Collection> list1 = new ArrayList<>();
        List<Collection> list2 = new ArrayList<>();
        for (Collection c:list
        ) {
            if(c.getSentenceId().equals("0")){
                topic = topicRepository.findById(c.getTopicId()).get();
                c.setTopicName(topic.getName());
                list1.add(c);
            }else if(c.getTopicId().equals("0")) {
                sentence = sentenceRepository.findById(c.getSentenceId()).get();
                c.setSentenceContent(sentence.getContent());
                list2.add(c);
            }
        }
        perfectCollection(list1);
        perfectCollection(list2);
        sortByTime(list1);
        sortByTime(list2);
        result.put("topicCollection", list1);
        result.put("sentenceCollection", list2);
        return result;
    }

    @Override
    public void addCollection(String userId, String sentenceId, String topicId){
        Collection collection = new Collection();
        collection.setId(SnowflakeIdWorker.generateId()+"");
        collection.setUserId(userId);
        collection.setTopicId(topicId);
        collection.setSentenceId(sentenceId);
        collection.setCreateAt(new Date());
        collection.setCreateBy("admin");
        collection.setUpdateAt(new Date());
        collection.setUpdateBy("admin");
        collection.setDel(false);
        collectionRepository.save(collection);
    }

    @Override
    public void removeCollection(String userId, String sentenceId, String topicId){
        Collection collection = new Collection();
        if(sentenceId.equals("0")){
            //删除主题
            collection = collectionRepository.findByUserIdAndTopicId(userId, topicId);
        }else if(topicId.equals("0")) {
            //删除句子
            collection = collectionRepository.findByUserIdAndSentenceId(userId, sentenceId);
        }
        try {
            collectionRepository.delete(collection);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 完善收藏对象信息
     * @param collection
     */
    private void perfectCollection(List<Collection> collection){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
        for (Collection c:collection
        ) {
            c.setCreateAtFormat(simpleDateFormat.format(c.getCreateAt()));
            c.setUpdateAtFormat(simpleDateFormat.format(c.getUpdateAt()));
        }
    }

    /**
     * 按时间排序
     * @param list
     */
    private void sortByTime(List<Collection> list){
        Collections.sort(list, new Comparator<Collection>() {
            @Override
            public int compare(Collection o1, Collection o2) {
                if(o1.getCreateAt().before(o2.getCreateAt())){
                    return 1;
                }else {
                    return -1;
                }
            }
        });
    }

}
